/**
 *
 */

var root = "/hub/v1";

var wbUtil = {
    st2:null,
    websocketGdsb: null,
    websocketGdsbList: {},
    websocketJk: null,
    initSocket: function(wsUrl, id, devid, devType, callback, reconnetcallback){
        let that = this;
        if (typeof WebSocket === 'undefined') {
            console.log('您的浏览器不支持WebSocket')
            return
        } else {
            // console.log('websocket:', url)
            // 打开一个websocket
            this.websocketGdsbList[id] = new WebSocket(wsUrl)
            // 建立连接
            this.websocketGdsbList[id].onopen = () => {
                that.onOpen(that, id, this.websocketGdsbList[id], wsUrl , devid, devType);
            }
            // 客户端接收服务端返回的数据
            this.websocketGdsbList[id].onmessage = (evt) => {
                that.onMessage(evt,id, this.websocketGdsbList[id], callback);
            }
            // 发生错误时
            this.websocketGdsbList[id].onerror = (evt) => {
                that.onErr(evt, this.websocketGdsbList[id], wsUrl, callback);
            }
            // 关闭连接
            this.websocketGdsbList[id].onclose = (evt) => {
                that.onClose(evt, id, this.websocketGdsbList[id], reconnetcallback);

            }
        }



    },
    onOpen(that, id, websocket, wsUrl, devid, devType){
        // 发送数据
        // this.websocket.send('发送数据')
        console.log('websocket:链接成功, url:'+ wsUrl  + ", devid = " + devid)
        setTimeout(function(){
            that.sdheartbeat(websocket, devid, devType)
        }, 1000*3)
        // that.heartbeat()
    },
    onMessage(evt,id, websocket,  callback){
        console.log('websocket返回的数据：', evt)
        var data = JSON.parse(evt.data)
        console.log(data)

        if(callback){
            callback(id, websocket, data);
        }



        if(data.data){//获取data字段
            // console.log(JSON.stringify(data.data))

        }

        console.log("接收数据:" + JSON.stringify(data) );
    },
    onErr(evt,  websocket,  wsUrl, callback){
        let that = this;
        console.log('websocket错误：', evt)
        setTimeout(function (){
            that.initSocket(wsUrl, callback)
        },5000)
    },
    onClose(evt,id,  websocket, reconnetcallback){
        console.log('websocket关闭：', evt)
        // setTimeout(initSocket, 1000)
        var data = {
            "data":"client断开连接"
        }
        console.log("client断开连接:" + JSON.stringify(data));
        if(reconnetcallback){
            reconnetcallback(id, websocket, data);
        }
    },
    sdheartbeat: function(websocket, devid, devType){
        let that = this;
        var json = {
            "devType": Number(devType),
            "devid": devid,
            "cmd": 31,
            "data": null
        }
        console.log('发送心跳................devid:' + devid)
        websocket.send(JSON.stringify(json))
        this.st2 = setTimeout(function(){
            that.sdheartbeat(websocket, devid, devType)
        }, 30*1000)
    },
    sendWsSpec: function(websocket, sendKey,devType, devid, cmd , sendValue, sdValue){


        if(sendKey){

            if(!websocket){
                console.log('ws对象不存在', websocket)
                return ;
            }
            var json = {
                "devType": Number(devType),
                "devid": devid,
                "cmd": Number(cmd),
                "data": {
                    "dtime"	: this.getCurrentTime()
                }
            }

            json['data'][sendKey] = sendValue;
            json['data']['speed'] = sdValue;
            // json['data']['ptzstop'] = 1;


            console.log("发送数据:" + JSON.stringify(json));
            websocket.send(JSON.stringify(json))

        }


    },
    sendWs: function (websocket, sendKey,devType, devid, cmd , sendValue){

        if(!websocket){
            console.log('ws对象不存在', websocket)
            return ;
        }
        var json = {
            "devType": Number(devType),
            "devid": devid,
            "cmd": Number(cmd),
            "data": {
                "dtime"	: this.getCurrentTime()
            }
        }

        json['data'][sendKey] = sendValue;
        console.log("发送数据:" + JSON.stringify(json));
        websocket.send(JSON.stringify(json))
    },
    getCurrentTime: function (){
        const currentTime = new Date();
        const year = currentTime.getFullYear();
        const month = currentTime.getMonth() + 1; // 月份从0开始，因此需要加1
        const day = currentTime.getDate();
        const hours = currentTime.getHours();
        const minutes = currentTime.getMinutes();
        const seconds = currentTime.getSeconds();
        return year + '.' + month + '.' + day + ' ' + (hours<=9 ? ('0'+hours) : hours) + ':'+(minutes<=9 ? ('0'+minutes) : minutes)+':' + (seconds<=9 ? ('0'+seconds) : seconds);
    }
}
